package com.zthz.douxing.sendmessageservice.service

import com.zthz.douxing.CityInfo
import com.zthz.douxing.TaskInfo
import com.zthz.douxing.TripSegment
import com.zthz.douxing.event.UserWillDepartEvent
import com.zthz.douxing.msg.MessageType
import com.zthz.douxing.msg.ServiceType
import com.zthz.douxing.system.PlatformType


abstract class AbstractHandlerService {

    def orderXingeMessageService
    def geTuiMessageSenderService

    protected formateTask2UserWillDepartEvent(TaskInfo taskInfo){

        UserWillDepartEvent userWillDepartEvent = new UserWillDepartEvent()
        TripSegment tripSegment = TripSegment.get(taskInfo?.boId)
        
        userWillDepartEvent.tripSegment = tripSegment
        userWillDepartEvent.cityCode = CityInfo.findByNameCn(tripSegment?.arriveCity)?.code
        userWillDepartEvent.getuiId = tripSegment?.plantrip?.personOwner?.getuiId
        userWillDepartEvent.username = tripSegment?.plantrip?.personOwner?.username
        userWillDepartEvent.platformType = PlatformType.android

        return userWillDepartEvent
    }

    protected void excute(TaskInfo taskInfo){

        String result = null
        try{
            log.info("current taskinfo is:"+taskInfo.id)
            result= hanldTask(taskInfo)
        }catch (Exception e){
            taskInfo.status = 3
            log.debug(e.getMessage(),e.printStackTrace())
        }

        if(result.contains("Ok")||result.contains("successed_online")
           ||result.contains("successed_offline")||result.contains("成功"))
            taskInfo.status = 2
        else
            taskInfo.status = 3

        taskInfo.save()
    }

    protected abstract String hanldTask (TaskInfo taskInfo)throws java.lang.Exception

    protected void failJob2Log(TaskInfo taskInfo){//记录执行状态更改失败的job到log 以便提供补救
        MessageType messageType = MessageType.ERRORSENDMESSAGEJOB
        String message = "消息发送"
        if(taskInfo.serviceType == ServiceType.changeArriveStatusService){
            messageType = MessageType.ERRORCHANGEARRIVEDJOB
            message ="更改行程段状态"
        }
        if(taskInfo.serviceType == ServiceType.changeBeginStatusService){
            messageType =ServiceType.changeBeginStatusService
            message ="更改行程段状态"
        }

        orderXingeMessageService.saveMessageToUserLog(
                taskInfo.boId,message,"失败",messageType)
    }
    protected boolean isNullMessage(String msg){
        if(msg.length()==0||msg.equals("")||msg == null)
            return true
    }
}
